#define _CRT_SECURE_NO_WARNINGS 1


 // Definition for a binary tree node.
  struct TreeNode {
      int val;
      TreeNode *left;
      TreeNode *right;
      TreeNode() : val(0), left(nullptr), right(nullptr) {}
      TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
      TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 };

#include <vector>
  using namespace std;
class Solution {
public:
    vector<int> postorderTraversal(TreeNode* root) {
        vector<int> retv;
        _postorderTraversal(root, retv);
        return retv;
    }

    void _postorderTraversal(TreeNode* root, vector<int>& v)
    {
        if (root == nullptr)
            return;
        if (root->left == nullptr && root->right == nullptr)
        {
            v.push_back(root->val);
            return;
        }
        if (root->left == nullptr)
        {
            _postorderTraversal(root->right, v);
            v.push_back(root->val);
            return;
        }
        if (root->right == nullptr)
        {
            _postorderTraversal(root->left, v);
            v.push_back(root->val);
            return;
        }
        _postorderTraversal(root->left, v);
        _postorderTraversal(root->right, v);
        v.push_back(root->val);
    }
};